/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.whozwho.web.rest;

import com.logiclander.whozwho.core.jaxrs.ext.base.NotFoundException;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.isNull;
import static com.google.common.base.Predicates.notNull;
import static com.logiclander.whozwho.web.rest.JaxbTypesFactory.createSystemTypeFromSystemDto;
import static com.logiclander.whozwho.web.rest.JaxbTypesFactory.createSystemsTypeFromSystemDtoCollection;
import static com.logiclander.whozwho.core.jaxrs.ext.base.WebPreconditions.checkWebArgument;
import static com.logiclander.whozwho.core.StringPredicates.IS_NOT_NULL_OR_BLANK;

import com.logiclander.whozwho.core.types.SystemType;
import com.logiclander.whozwho.core.types.SystemsType;
import com.logiclander.whozwho.data.dao.SystemDao;
import com.logiclander.whozwho.data.dao.SystemDto;
import java.util.List;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

/**
 *
 * @author logiclander
 */
@Path("/")
public class SystemController {

  private SystemDao systemDao;

  public void setSystemDao(SystemDao systemDao) {
    checkState(isNull().apply(this.systemDao),
            "SystemDao property has already been set.");
    this.systemDao = checkNotNull(systemDao,
            "SystemDao property cannot be set to null.");
  }

  public void init() {
    checkState(notNull().apply(systemDao),
            "Required SystemDao property not set.");
  }

  @GET
  @Path("/systems")
  @Produces(MediaType.APPLICATION_XML)
  //when there are query parameters then searchSystems()
  //otherwise browseSystems()

// may want to add filters for other attributes later...
//          @QueryParam("roleSidFilter") @DefaultValue("") String roleSidFilter,
//          @QueryParam("roleTitleFilter") @DefaultValue("") String roleTitleFilter,
//          @QueryParam("appSidFilter") @DefaultValue("") String appSidFilter,
//          @QueryParam("appTitleFilter") @DefaultValue("") String appTitleFilter

//  public SystemsType getSystemList(@Context UriInfo uriInfo,
  public SystemsType getSystems(@Context UriInfo uriInfo,
          @QueryParam("sidFilter") @DefaultValue("") String sidFilter,
          @QueryParam("titleFilter") @DefaultValue("") String titleFilter) {

    checkWebArgument(IS_NOT_NULL_OR_BLANK.apply(sidFilter) != IS_NOT_NULL_OR_BLANK.apply(titleFilter),
            "Bad parameters specified; either missing both or both given.");

    List<SystemDto> systems = null;

    if (IS_NOT_NULL_OR_BLANK.apply(sidFilter)) {
      systems = systemDao.getSystemsBySidFilter(sidFilter);
      if (SystemDto.EMPTY_SYSTEM_DTO.equals(systems)) {
        throw new NotFoundException("System(s) with that id filter does not exist.");
      }
    }

    if (IS_NOT_NULL_OR_BLANK.apply(titleFilter)) {
      systems = systemDao.getSystemsByTitleFilter(titleFilter);
      if (SystemDto.EMPTY_SYSTEM_DTO.equals(systems)) {
        throw new NotFoundException("System(s) with that title filter does not exist.");
      }
    }

    return createSystemsTypeFromSystemDtoCollection(uriInfo, systems);

  }

  @GET
  @Path("/system/{sid}")
  @Produces(MediaType.APPLICATION_XML)
  public SystemType getSystemBySid(@Context UriInfo uriInfo,
          @PathParam("sid") Long sid) {

    checkWebArgument(notNull().apply(sid), "Missing sid.");

    SystemDto system = systemDao.getSystemBySid(sid);

    if (SystemDto.EMPTY_SYSTEM_DTO.equals(system)) {
      throw new NotFoundException("System with that sid does not exist.");
    }

    return createSystemTypeFromSystemDto(uriInfo, system);

  }
  
}
